<!doctype html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>删除链表的倒数第 N 个节点 - 算法详解</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
        }
        .serif-font {
            font-family: 'Noto Serif SC', serif;
        }
        .gradient-text {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
        }
        .card-hover {
            transition: all 0.3s ease;
        }
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
        }
        .code-block {
            background: linear-gradient(135deg, #1e1e2e 0%, #2d2d44 100%);
        }
        .highlight-box {
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
        }
        @keyframes fadeIn {
            from { opacity: 0; transform: translateY(20px); }
            to { opacity: 1; transform: translateY(0); }
        }
        .fade-in {
            animation: fadeIn 0.8s ease-out;
        }
        .step-number {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        pre code {
            font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
        }
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="relative overflow-hidden bg-gradient-to-br from-purple-600 via-indigo-600 to-blue-600 text-white">
        <div class="absolute inset-0 bg-black opacity-20"></div>
        <div class="relative container mx-auto px-6 py-24">
            <div class="max-w-4xl mx-auto text-center fade-in">
                <div class="mb-6">
                    <i class="fas fa-link text-6xl opacity-80"></i>
                </div>
                <h1 class="text-5xl md:text-6xl font-bold mb-6 serif-font">
                    删除链表的倒数第 N 个节点
                </h1>
                <p class="text-xl md:text-2xl opacity-90 leading-relaxed">
                    掌握快慢指针技巧，优雅解决链表问题
                </p>
                <div class="mt-8 flex justify-center space-x-6">
                    <span class="inline-flex items-center px-4 py-2 bg-white bg-opacity-20 rounded-full">
                        <i class="fas fa-clock mr-2"></i>
                        时间复杂度 O(n)
                    </span>
                    <span class="inline-flex items-center px-4 py-2 bg-white bg-opacity-20 rounded-full">
                        <i class="fas fa-memory mr-2"></i>
                        空间复杂度 O(1)
                    </span>
                </div>
            </div>
        </div>
        <div class="absolute bottom-0 left-0 right-0">
            <svg viewBox="0 0 1440 120" fill="none" xmlns="http://www.w3.org/2000/svg">
                <path d="M0 120L60 110C120 100 240 80 360 70C480 60 600 60 720 65C840 70 960 80 1080 85C1200 90 1320 90 1380 90L1440 90V120H1380C1320 120 1200 120 1080 120C960 120 840 120 720 120C600 120 480 120 360 120C240 120 120 120 60 120H0V120Z" fill="#F9FAFB"/>
            </svg>
        </div>
    </section>

    <!-- Main Content -->
    <main class="container mx-auto px-6 py-16 max-w-6xl">
        <!-- Problem Description -->
        <section class="mb-16 fade-in">
            <div class="bg-white rounded-2xl shadow-xl p-8 card-hover">
                <div class="flex items-center mb-6">
                    <div class="w-12 h-12 bg-gradient-to-br from-purple-500 to-indigo-600 rounded-full flex items-center justify-center text-white mr-4">
                        <i class="fas fa-question-circle text-xl"></i>
                    </div>
                    <h2 class="text-3xl font-bold text-gray-800">题目描述</h2>
                </div>
                <div class="prose prose-lg max-w-none">
                    <p class="text-gray-700 leading-relaxed text-lg">
                        给定一个链表，删除链表的倒数第 n 个节点，并返回链表的头节点。
                    </p>
                    <div class="mt-6 p-6 bg-gradient-to-r from-purple-50 to-indigo-50 rounded-xl">
                        <p class="font-semibold text-gray-800 mb-3">
                            <i class="fas fa-lightbulb text-yellow-500 mr-2"></i>
                            示例：
                        </p>
                        <div class="bg-white p-4 rounded-lg shadow-inner">
                            <p class="font-mono text-lg">
                                输入：1→2→3→4→5, n = 2<br>
                                输出：1→2→3→5
                            </p>
                        </div>
                        <p class="mt-4 text-sm text-gray-600">
                            <i class="fas fa-info-circle mr-1"></i>
                            注意：给定的 n 总是有效的
                        </p>
                    </div>
                </div>
            </div>
        </section>

        <!-- Core Algorithm -->
        <section class="mb-16 fade-in">
            <div class="bg-gradient-to-r from-indigo-500 to-purple-600 rounded-2xl shadow-xl p-8 text-white">
                <div class="flex items-center mb-6">
                    <div class="w-12 h-12 bg-white bg-opacity-20 rounded-full flex items-center justify-center mr-4">
                        <i class="fas fa-brain text-2xl"></i>
                    </div>
                    <h2 class="text-3xl font-bold">核心算法</h2>
                </div>
                <div class="text-center py-8">
                    <h3 class="text-5xl font-bold mb-4 serif-font">快慢指针</h3>
                    <p class="text-xl opacity-90">Two Pointers Technique</p>
                </div>
            </div>
        </section>

        <!-- Solution Approach -->
        <section class="mb-16">
            <div class="bg-white rounded-2xl shadow-xl p-8">
                <div class="flex items-center mb-8">
                    <div class="w-12 h-12 bg-gradient-to-br from-green-500 to-teal-600 rounded-full flex items-center justify-center text-white mr-4">
                        <i class="fas fa-route text-xl"></i>
                    </div>
                    <h2 class="text-3xl font-bold text-gray-800">解题思路</h2>
                </div>
                
                <div class="grid md:grid-cols-2 gap-8 mb-8">
                    <div class="space-y-6">
                        <div class="flex items-start">
                            <div class="step-number w-10 h-10 rounded-full flex items-center justify-center text-white font-bold mr-4 flex-shrink-0">
                                1
                            </div>
                            <div>
                                <h4 class="font-semibold text-lg mb-2">初始化双指针</h4>
                                <p class="text-gray-600">创建虚拟头节点，快慢指针都指向虚拟头节点</p>
                            </div>
                        </div>
                        
                        <div class="flex items-start">
                            <div class="step-number w-10 h-10 rounded-full flex items-center justify-center text-white font-bold mr-4 flex-shrink-0">
                                2
                            </div>
                            <div>
                                <h4 class="font-semibold text-lg mb-2">快指针先行</h4>
                                <p class="text-gray-600">快指针先向前移动 n 步，建立间隔</p>
                            </div>
                        </div>
                        
                        <div class="flex items-start">
                            <div class="step-number w-10 h-10 rounded-full flex items-center justify-center text-white font-bold mr-4 flex-shrink-0">
                                3
                            </div>
                            <div>
                                <h4 class="font-semibold text-lg mb-2">同步移动</h4>
                                <p class="text-gray-600">快慢指针同时移动，直到快指针到达末尾</p>
                            </div>
                        </div>
                        
                        <div class="flex items-start">
                            <div class="step-number w-10 h-10 rounded-full flex items-center justify-center text-white font-bold mr-4 flex-shrink-0">
                                4
                            </div>
                            <div>
                                <h4 class="font-semibold text-lg mb-2">删除节点</h4>
                                <p class="text-gray-600">慢指针指向待删除节点的前一个节点，执行删除操作</p>
                            </div>
                        </div>
                    </div>
                    
                    <div class="bg-gradient-to-br from-gray-50 to-gray-100 rounded-xl p-6">
                        <h4 class="font-semibold text-lg mb-4 text-center">算法可视化</h4>
                        <div class="mermaid">
                            graph LR
                                A[dummy] --> B[1]
                                B --> C[2]
                                C --> D[3]
                                D --> E[4]
                                E --> F[5]
                                F --> G[null]
                                
                                style A fill:#e0e7ff,stroke:#6366f1
                                style E fill:#fecaca,stroke:#ef4444
                                style C fill:#d1fae5,stroke:#10b981
                        </div>
                    </div>
                </div>

                <div class="mt-8 p-6 bg-blue-50 rounded-xl">
                    <div class="flex items-center mb-3">
                        <i class="fas fa-lightbulb text-blue-600 text-xl mr-3"></i>
                        <h4 class="font-semibold text-lg">关键洞察</h4>
                    </div>
                    <p class="text-gray-700">
                        通过维持两个指针之间的固定间隔（n个节点），当快指针到达链表末尾时，慢指针恰好位于倒数第n个节点的前一个位置。
                    </p>
                </div>
            </div>
        </section>

        <!-- Code Implementation -->
        <section class="mb-16">
            <div class="bg-white rounded-2xl shadow-xl overflow-hidden">
                <div class="bg-gradient-to-r from-gray-800 to-gray-900 p-6">
                    <div class="flex items-center justify-between">
                        <div class="flex items-center">
                            <div class="w-10 h-10 bg-white bg-opacity-20 rounded-full flex items-center justify-center text-white mr-4">
                                <i class="fas fa-code text-lg"></i>
                            </div>
                            <h2 class="text-2xl font-bold text-white">代码实现</h2>
                        </div>
                        <span class="px-4 py-2 bg-white bg-opacity-20 rounded-full text-white text-sm">
                            Python
                        </span>
                    </div>
                </div>
                
                <div class="code-block p-6 overflow-x-auto">
                    <pre class="text-gray-100"><code class="language-python">def removeNthFromEnd(head, n):
    # 创建虚拟头节点，简化边界处理
    dummy = ListNode(0)
    dummy.next = head
    
    # 初始化快慢指针
    fast = slow = dummy
    
    # 快指针先走 n 步
    for _ in range(n):
        fast = fast.next
    
    # 快慢指针同时移动
    while fast.next:
        fast = fast.next
        slow = slow.next
    
    # 删除倒数第 n 个节点
    slow.next = slow.next.next
    
    return dummy.next</code></pre>
                </div>
                
                <div class="p-6 bg-gray-50">
                    <div class="grid md:grid-cols-3 gap-4">
                        <div class="text-center p-4 bg-white rounded-lg shadow">
                            <i class="fas fa-tachometer-alt text-3xl text-green-500 mb-2"></i>
                            <p class="font-semibold">时间复杂度</p>
                            <p class="text-2xl font-bold text-gray-800">O(n)</p>
                        </div>
                        <div class="text-center p-4 bg-white rounded-lg shadow">
                            <i class="fas